package com.eCommerce.server.svco.impl;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.transaction.annotation.Transactional;

import com.eCommerce.corecontract.criteria.CategorieCriteria;
import com.eCommerce.corecontract.dto.CategorieDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommerceSVCOException;
import com.eCommerce.server.corebackend.sisv.ICategorieSISV;
import com.eCommerce.server.corebackend.sisv.SISVFinder;
import com.eCommerce.server.corebackend.util.BackEndConstants;
import com.eCommercecorecontract.svco.ICategorieSVCO;
@Transactional
public class CategorieSVCOImpl extends AbstractCommonSVCO implements ICategorieSVCO, Serializable{

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SISVFinder sisvFinder;
	private ICategorieSISV categorieSISV;
	private static Logger log =Logger.getLogger(CategorieSVCOImpl.class);
	public CategorieSVCOImpl(SISVFinder sisvFinder) throws Exception
	{
		this.sisvFinder=sisvFinder;
		categorieSISV=(ICategorieSISV)sisvFinder.get(BackEndConstants.SISV_CATEGORIE);
		
	}
	public Long ajouterCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			return categorieSISV.ajouterCategorie(categorieDTO);
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception e) {
			Object[] arg1 = new Object[1];
			arg1[0]=categorieDTO.getLibelle();//.getNom();
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOCATEGORIE-001", arg1, e.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
	}

	public void modifierCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		try {
			 categorieSISV.modifierrCategorie(categorieDTO);
			
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception e) {
			Object[] arg1 = new Object[1];
			arg1[0]=categorieDTO.getLibelle();//.getNom();
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOCATEGORIE-002", arg1, e.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
		
	}

	public List rechercherCategorie(CategorieCriteria categorieCriteria)
			throws EcommerceException {
		try {
			return categorieSISV.rechercherCategorie(categorieCriteria);
			
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception e) {
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOCATEGORIE-004", arg1, e.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
	}

	public void supprimerCategorie(Set<CategorieDTO> lifOfCategorieDTO)
			throws EcommerceException {
		try {
			// for(CategorieDTO categorieDTO:lifOfCategorieDTO) 
				 Iterator i=listerCategorie().iterator();
				 while (i.hasNext())
				 {
					CategorieDTO categorieDTO= (CategorieDTO)i.next();
				 	categorieSISV.supprimerCategorie(categorieDTO);
				 }
			
			
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception e) {
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOCATEGORIE-005", arg1, e.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
		
	}

	public CategorieDTO visualiserCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		try {
			 return categorieSISV.visualiserCategorie(categorieDTO);
			
			
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception e) {
			Object[] arg1 = new Object[1];
			arg1[0]=categorieDTO.getLibelle();//.getNom();
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOCATEGORIE-003", arg1, e.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
	}
	public List listerCategorie() throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			return categorieSISV.listerCategorie();
						
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception e) {
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOCATEGORIE-004", arg1, e.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
	}
	public void supprimerCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		try {
			
					
				 	categorieSISV.supprimerCategorie(categorieDTO);
				 
			
			
		} 
		catch (EcommerceException ex)
		{
			throw ex;
		}
		catch (Exception e) {
			Object[] arg1 = new Object[1];
			
			EcommerceSVCOException svcoException = new EcommerceSVCOException(messagesXmlFileName, "ECOMMERCESVCOCATEGORIE-005", arg1, e.getMessage());
			log.error(svcoException.getMessage());
			throw svcoException;
		}
		
	}

}
